//
//  CertifySelectViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/8/20.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "CertifySelectViewController.h"
#import "RealNameContentViewController.h"

@interface CertifySelectViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITableViewCell *cell;
@property (nonatomic, strong) NSArray *infoArr;

@end

@implementation CertifySelectViewController

- (void)viewWillAppear:(BOOL)animated
{
	[self updateUserInfo];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	_infoArr = @[@"求职者认证",@"商家认证",@"企业认证"];
	_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
	[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.dataSource = self;
    _tableView.delegate = self;
	[self creatUI];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)creatUI {
	self.navigationItem.title = @"认证选择";
	self.view.backgroundColor = UISeparatedColorRGB;
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = self.infoArr[indexPath.row];
    cell.textLabel.textColor = kGrayColor;
    UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-SW(12), cell.height/4, cell.height/3, cell.height/2)];
    [arrow setImage:IMAGE(@"arrow_right_grey")];
    [cell addSubview:arrow];
	int roleNum = [GetUserDefaule(USERINFO)[@"role"] intValue];
	if (roleNum == 2 || roleNum == 4) {
		if (indexPath.row == 0) {
			UIImageView *rightIcon = [UIImageView imageViewWithImage:IMAGE(@"id_chose")];
			rightIcon.frame = CGRectMake(110, 0, 15, 10);
			rightIcon.centerY = cell.contentView.centerY;
			[cell.contentView addSubview:rightIcon];
		}
	}
	if (roleNum == 3 || roleNum == 4 || roleNum == 6) {
		if (indexPath.row == 1) {
			UIImageView *rightIcon = [UIImageView imageViewWithImage:IMAGE(@"id_chose")];
			rightIcon.frame = CGRectMake(95, 0, 15, 10);
			rightIcon.centerY = cell.contentView.centerY;
			[cell.contentView addSubview:rightIcon];
		}
	}
	if (roleNum == 5 || roleNum == 6) {
		if (indexPath.row == 2) {
			UIImageView *rightIcon = [UIImageView imageViewWithImage:IMAGE(@"id_chose")];
			rightIcon.frame = CGRectMake(95, 0, 15, 10);
			rightIcon.centerY = cell.contentView.centerY;
			[cell.contentView addSubview:rightIcon];
		}
	}
	return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return self.infoArr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RealNameContentViewController *normal = [[RealNameContentViewController alloc] init];
    switch (indexPath.row) {
        case 0:
            normal.roleToApply = @2;
            [self.navigationController pushViewController:normal animated:YES];
            break;
        case 1:
            normal.roleToApply = @3;
            [self.navigationController pushViewController:normal animated:YES];
            break;
        case 2:
            normal.roleToApply = @5;
            [self.navigationController pushViewController:normal animated:YES];
            break;
            
        default:
            break;
    }
}


/**
 更新用户信息
 */
- (void)updateUserInfo {
	
	[[NetworkTool sharedTool] requestWithName:@"获取用户信息" URLString:APIUser parameters:nil method:@"GET" callBack:^(id responseObject) {
		NSDictionary *userDict = [responseObject valueForKey:@"data"];
		SetUserDefaule(userDict, USERINFO);
		NSLog(@"user info in per is %@", userDict);
		if ([GetUserDefaule(USERINFO)[@"role"] integerValue] == 0) {
			[self presentViewController:[LZCTools alertMessage:@"请先登录"]animated:YES completion:nil];
			return;
		}
	}];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
